//
//  HMEmoticonToolbar.swift
//  WeiBo14
//
//  Created by HM on 16/3/12.
//  Copyright © 2016年 HM. All rights reserved.
//  底部切换表情类型的toolBar

import UIKit

/// 表情类型
///
/// - Recent:  最近
/// - Default: 默认
/// - Emoji:   Emoji description
/// - Lxh:     浪小花
enum HMEmotionType: Int {
    case Recent = 0, Default, Emoji, Lxh
}


protocol HMEmoticonToolbarDelegate: NSObjectProtocol {
    
    // 哪一个类型的表情按钮选中了的代理方法
    func emoticonToolar(toolBar: HMEmoticonToolbar, type emotionTypeSelected: HMEmotionType)
    
}


class HMEmoticonToolbar: UIStackView {

    // 上一次选中的按钮
    var lastSelectedButton: UIButton?
    
    // 代理
    weak var delegate: HMEmoticonToolbarDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// 选中section对应的按钮
    ///
    /// - parameter section: <#section description#>
    func selectedButtonWithSection(section: Int) {
        
        // 1. 取到section对应的按钮
        let button = viewWithTag(section)! as! UIButton
        
        if button == lastSelectedButton {
            return
        }
        
        // 2. 选中该按钮
        button.selected = true
        // 3. 让之前的按钮取消选中
        lastSelectedButton?.selected = false
        // 4. 记录当前选中的按钮,供下次使用
        lastSelectedButton = button
    }
    
    private func setupUI(){
        // 调整当前的tag
        tag = 1000
        // 设置每个按钮大小相等
        distribution = .FillEqually
        
        addChildButton("最近", bgImageNamed: "compose_emotion_table_left", type: .Recent)
        addChildButton("默认", bgImageNamed: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoji", bgImageNamed: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", bgImageNamed: "compose_emotion_table_right", type: .Lxh)
    }
    
    
    /// 添加子按钮的方法
    ///
    /// - parameter title:        <#title description#>
    /// - parameter bgImageNamed: <#bgImageNamed description#>
    private func addChildButton(title: String, bgImageNamed: String, type: HMEmotionType) {
        
        let button = UIButton()
        
        // 给按钮添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置按钮的tag,以便区分哪个按钮点击了
        button.tag = type.rawValue
        
        // 设置文字以及不同状态下的背景图片
        button.setTitle(title, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImageNamed)_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImageNamed)_selected"), forState: .Selected)
        
        // 设置字体大小和不同状态下的文字颜色
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        
        // 添加控件
        addArrangedSubview(button)
    }
    
    // 按钮点击
    @objc private func buttonClick(button: UIButton){
        
        // 上一次选中的按钮就是本次选中的按钮,直接不做任何操作
        if lastSelectedButton == button {
            return
        }
        
        // 取到按钮的tag,生成一个枚举,并且告诉外界,我哪种类型的表情按钮点击了
        delegate?.emoticonToolar(self, type: HMEmotionType(rawValue: button.tag)!)
        
        // 让button选中,
        button.selected = true
        // 让一次的选中的按钮取消选中
        lastSelectedButton?.selected = false
        // 记录当前选中的按钮,以便在下一次点击的取消选中
        lastSelectedButton = button
    }

}
